package com.project.beagentleman;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Random;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

	private TextView tipOfTheDay;

	private Button lessons;
	private Button about;
	private Button exit;
	private Button help;

	private Context context;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);

		TextView txt1 = (TextView) findViewById(R.id.lessons);  
		Typeface lessons_font = Typeface.createFromAsset(getAssets(), "arial.ttf");  
		txt1.setTypeface(lessons_font);

		TextView txt2 = (TextView) findViewById(R.id.help);  
		Typeface help_font = Typeface.createFromAsset(getAssets(), "arial.ttf");  
		txt2.setTypeface(help_font);

		TextView txt3 = (TextView) findViewById(R.id.about);  
		Typeface about_font = Typeface.createFromAsset(getAssets(), "arial.ttf");  
		txt3.setTypeface(about_font);

		TextView txt4 = (TextView) findViewById(R.id.exit);  
		Typeface exit_font = Typeface.createFromAsset(getAssets(), "arial.ttf");  
		txt4.setTypeface(exit_font);
		
		initComponents();
	}
	
	public void initComponents() {
		context = this;

		tipOfTheDay = (TextView) findViewById(R.id.tipOfTheDayTV);
		RotateAnimation rotate = (RotateAnimation)AnimationUtils.loadAnimation(this, R.anim.textdialogal);
		tipOfTheDay.setAnimation(rotate);

		readTipOfTheDay();

		lessons = (Button) findViewById(R.id.lessons);
		lessons.setBackgroundResource(R.xml.lessons_button_selector);
		lessons.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				startActivity(LessonsActivity.class);
			}
		});

		about = (Button) findViewById(R.id.about);
		about.setBackgroundResource(R.xml.about_button_selector);
		about.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				AboutDialogDisplayer aboutDialogDisplayer = new AboutDialogDisplayer(context);
				aboutDialogDisplayer.setTitle("Rólunk");
				aboutDialogDisplayer.show();
			}
		});

		help = (Button) findViewById(R.id.help);
		help.setBackgroundResource(R.xml.help_button_selector);
		help.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				HelpDialogDisplayer helpDialogDisplayer = new HelpDialogDisplayer(context);
				helpDialogDisplayer.setTitle("Segítség");
				helpDialogDisplayer.show();

			}
		});

		exit = (Button) findViewById(R.id.exit);
		exit.setBackgroundResource(R.xml.exit_button_selector);
		exit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	public void readTipOfTheDay() {
		InputStream inputStream = getResources().openRawResource(R.raw.tips_hu);

		BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
		String line;
		try {
			line = reader.readLine();
			Random random = new Random();
			int randomNumber = random.nextInt(Integer.parseInt(line));
			int i = 0;

			while ((line = reader.readLine()) != null) {
				if (i == randomNumber) {
					tipOfTheDay.setText(line);
					break;
				}
				i++;
			}

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void startActivity(Class<?> activityClass) {
		startActivity(new Intent(this, activityClass));
	}
}
